package demoMod.stsconsole.saves;

import com.google.gson.JsonElement;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.interfaces.CustomSavable;
import demoMod.stsconsole.utils.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根存档器，存档/读档功能的入口点
 */
public class RootSaver extends InstanceDataSaver<RootSaver, Map<String, Object>> implements CustomSavable<Map<String, Object>> {
    public static List<InstanceDataSaver<? extends CustomSavable<?>, ?>> customSavers = new ArrayList<>();

    @Override
    public Map<String, Object> onSave() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("player", AbstractDungeon.player.onSave());
        ret.put("dungeon", AbstractDungeon.currDungeon.onSave());
        List<Object> customSaves = new ArrayList<>();
        for (InstanceDataSaver<? extends CustomSavable<?>, ?> customSaver : customSavers) {
            customSaves.add(customSaver.onSave(null));
        }
        ret.put("customSaves", customSaves);
        return ret;
    }

    @Override
    public void onLoad(Map<String, Object> data) {
        if (data != null) { //加载存档
            if (data.containsKey("player")) {
                AbstractDungeon.player.onLoad(JsonParser.fromJson((JsonElement) data.get("player"), JsonParser.stringObjectMap));
            } else {
                AbstractDungeon.player.onLoad(null);
            }
            if (data.containsKey("dungeon")) {
                Map<String, Object> dungeonSave = JsonParser.fromJson((JsonElement) data.get("dungeon"), JsonParser.stringObjectMap);
                AbstractDungeon.seed = (long) dungeonSave.get("seed");
                AbstractDungeon.initializeRng(AbstractDungeon.seed);
                AbstractDungeon.currDungeon = AbstractDungeon.getDungeon((String) dungeonSave.get("id"));
                AbstractDungeon.currDungeon.onLoad(dungeonSave);
            } else {
                AbstractDungeon.seed = System.currentTimeMillis();
                AbstractDungeon.initializeRng(AbstractDungeon.seed);
                AbstractDungeon.currDungeon = AbstractDungeon.getDungeon(1);
                AbstractDungeon.currDungeon.onLoad(null);
            }
            if (data.containsKey("customSaves")) {
                for (InstanceDataSaver customSaver : customSavers) {
                    customSaver.onLoad(null, data.get("customSaves"));
                }
            } else {
                for (InstanceDataSaver customSaver : customSavers) {
                    customSaver.onLoad(null, null);
                }
            }
        } else { //新开局
            AbstractDungeon.player.onLoad(null);
            AbstractDungeon.seed = System.currentTimeMillis();
            AbstractDungeon.initializeRng(AbstractDungeon.seed);
            AbstractDungeon.currDungeon = AbstractDungeon.getDungeon(1);
            AbstractDungeon.currDungeon.onLoad(null);
            for (InstanceDataSaver customSaver : customSavers) {
                customSaver.onLoad(null, null);
            }
        }
    }
}
